//
//  WmcHomeControlAssistLineView.swift
//  YPWatermarkCamera
//
//  Created by 赵新 on 2022/7/15.
//

import UIKit

extension WmcHomeControlAssistLineLayer {
    /// 辅助线类型
    enum AssistLineStyle: Int, CaseIterable {
        /// 无
        case none

        /// 九宫格
        case nineBox

        /// 十字线
        case crossBox

        /// 准星
        case sight

        /// 圆圈
        case round

        struct Info {
            var name: String
        }

        var info: Info {
            switch self {
            case .none: return .init(name: "无")
            case .nineBox: return .init(name: "九宫格")
            case .crossBox: return .init(name: "十字线")
            case .sight: return .init(name: "准星")
            case .round: return .init(name: "圆形")
            }
        }
    }
}

// MARK: - [WmcHomeControlAssistLineLayer]

class WmcHomeControlAssistLineLayer: CAShapeLayer {
    /// 辅助线类型
    var assistLineType: AssistLineStyle = .none {
        didSet { setNeedsLayout() }
    }

    override func layoutSublayers() {
        super.layoutSublayers()

        let width = frame.width + lineWidth * 2
        let height = frame.height + lineWidth * 2

        let bezierPath = UIBezierPath()
        switch assistLineType {
        // 九宫格
        case .nineBox:
            let thirdPartW = frame.width / 3
            let thirdPartH = frame.height / 3
            let horizontalPath = UIBezierPath(rect: .init(x: -lineWidth, y: thirdPartH, width: width, height: thirdPartH))
            bezierPath.append(horizontalPath)
            let verticalPath = UIBezierPath(rect: .init(x: thirdPartW, y: -lineWidth, width: thirdPartW, height: height))
            bezierPath.append(verticalPath)
        // 十字线
        case .crossBox:
            let secondPartW = frame.width / 2
            let secondPartH = frame.height / 2
            bezierPath.move(to: .init(x: -lineWidth, y: secondPartH))
            bezierPath.addLine(to: .init(x: width, y: secondPartH))

            bezierPath.move(to: .init(x: secondPartW, y: -lineWidth))
            bezierPath.addLine(to: .init(x: secondPartW, y: height))

        // 准星
        case .sight:
            let size: CGFloat = 40
            let secondPartW = frame.width / 2
            let secondPartH = frame.height / 2
            let partW = (frame.width - size) / 2
            let partH = (frame.height - size) / 2
            bezierPath.move(to: .init(x: partW, y: secondPartH))
            bezierPath.addLine(to: .init(x: partW + size, y: secondPartH))

            bezierPath.move(to: .init(x: secondPartW, y: partH))
            bezierPath.addLine(to: .init(x: secondPartW, y: partH + size))

        // 圆圈
        case .round:
            let radius: CGFloat = 40
            let secondPartW = frame.width / 2
            let secondPartH = frame.height / 2
            let roundPath = UIBezierPath(arcCenter: .init(x: secondPartW, y: secondPartH),
                                         radius: radius,
                                         startAngle: 0,
                                         endAngle: CGFloat.pi * 2,
                                         clockwise: true)
            bezierPath.append(roundPath)

        default: break
        }
        path = bezierPath.cgPath
    }
}
